package mamma.action;

import java.util.List;
import java.util.Map;

import mamma.dao.AccountDao;
import mamma.dao.FriendsDao;
import mamma.vo.FriendUpdateVo;
import mamma.vo.MemberVo;
import mamma.vo.PhotoVo;
import mamma.vo.SearchVo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FriendsAction extends ActionSupport {
	
	private FriendUpdateVo friendVo;
	private SearchVo searchVo;
	private MemberVo memberVo;
	
	private String email;						// the value that brings the user's info to the login page.
	private String loginId;						// the value that has the login info.
	private String requestEmail;
	private String keyword;						// the value used when the user searchs mamma members.
	
	private List<SearchVo> searchList;			// the list that include the result of searching members.
	private List<SearchVo> searchDetailList;
	private List<FriendUpdateVo> friendUpdateList; // the list that include the alarm of friend requests.
	private List<FriendUpdateVo> myRequestList;
	
	private int friendId;
	
	/*
	 * FRIENDS ACTION
	 */
	
	// search member
	public String searchMember(){
		System.out.println("searchMember method activated");
		AccountDao dao = new AccountDao();
		searchList = dao.searchMember(keyword);
		
		return SUCCESS;
	}
	
	// search members in detail
	public String searchMemberDetail(){
		System.out.println("searchMemberDetail method activated");
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		searchVo.setEmail(temp.getEmail());
		
		FriendsDao friendsDao = new FriendsDao();
		searchDetailList = friendsDao.searchMemberDetail(searchVo);
		
		return SUCCESS;
	}
	
	public String goFindFriend(){
		System.out.println("goFindFriend method activated");
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		memberVo = (MemberVo)session.get("login");
		loginId = memberVo.getEmail();
		
		FriendsDao friendsDao = new FriendsDao();
		friendUpdateList = friendsDao.selectFriendsAlerts(loginId);
		myRequestList = friendsDao.selectMyRequest(loginId);
		
		return SUCCESS;
	}
	
	public String friendAllow(){
		System.out.println("friendAllow method activated");
		
		AccountDao dao = new AccountDao();
		FriendUpdateVo vo = new FriendUpdateVo();
		vo.setId(friendId);
		vo.setRequestEmail(requestEmail);
		
		FriendsDao friendsDao = new FriendsDao();
		int result = friendsDao.friendAllow(vo);
		
		return SUCCESS;
	}
	
	public String sendRequest(){
		System.out.println("sendRequest method activated");
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		
		FriendUpdateVo vo = new FriendUpdateVo();
		vo.setEmail(email);					// reciever
		vo.setRequestEmail(temp.getEmail());	// sender
		FriendsDao friendsDao = new FriendsDao();
		friendsDao.sendRequest(vo);
		
		return SUCCESS;
	}
	
	public String cancelRequest(){
		System.out.println("cancelRequest method activated");
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		FriendUpdateVo vo = new FriendUpdateVo();
		vo.setEmail(email);					// reciever
		vo.setRequestEmail(temp.getEmail());	// sender
		FriendsDao friendsDao = new FriendsDao();
		friendsDao.cancelRequest(vo);
		
		return SUCCESS;
	}
	
	// this method checks the relationship of other users
	public FriendUpdateVo checkRequest(String email, String requestEmail){
		System.out.println("checkRequest method activated");
		
		AccountDao dao = new AccountDao();
		FriendUpdateVo vo = new FriendUpdateVo();
		vo.setEmail(email);					// reciever
		vo.setRequestEmail(requestEmail);	// sender
		
		FriendsDao friendsDao = new FriendsDao();
		friendVo = friendsDao.checkRequest(vo);
		System.out.println("friendVo: "+friendVo);
		return friendVo;
	}

	public FriendUpdateVo getFriendVo() {
		return friendVo;
	}

	public void setFriendVo(FriendUpdateVo friendVo) {
		this.friendVo = friendVo;
	}

	public SearchVo getSearchVo() {
		return searchVo;
	}

	public void setSearchVo(SearchVo searchVo) {
		this.searchVo = searchVo;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getRequestEmail() {
		return requestEmail;
	}

	public void setRequestEmail(String requestEmail) {
		this.requestEmail = requestEmail;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List<SearchVo> getSearchList() {
		return searchList;
	}

	public void setSearchList(List<SearchVo> searchList) {
		this.searchList = searchList;
	}

	public List<SearchVo> getSearchDetailList() {
		return searchDetailList;
	}

	public void setSearchDetailList(List<SearchVo> searchDetailList) {
		this.searchDetailList = searchDetailList;
	}

	public List<FriendUpdateVo> getFriendUpdateList() {
		return friendUpdateList;
	}

	public void setFriendUpdateList(List<FriendUpdateVo> friendUpdateList) {
		this.friendUpdateList = friendUpdateList;
	}

	public List<FriendUpdateVo> getMyRequestList() {
		return myRequestList;
	}

	public void setMyRequestList(List<FriendUpdateVo> myRequestList) {
		this.myRequestList = myRequestList;
	}

	public int getFriendId() {
		return friendId;
	}

	public void setFriendId(int friendId) {
		this.friendId = friendId;
	}

	public MemberVo getMemberVo() {
		return memberVo;
	}

	public void setMemberVo(MemberVo memberVo) {
		this.memberVo = memberVo;
	}
	
}
